﻿using CommonDevelop;
using CommonDevelop.DB;
using ManageDB;
using ManageDto;
using ManageRepository;
using System.Collections.Generic;

namespace ManageService
{
    /// <summary>
    /// 管理图片接口
    /// </summary>
    public interface IPicService : IBaseService<Base_Picture>
    {
        List<PicDto> ShowPic(string PicGroup_No);
        void SavePic(PicDto Entity);
        List<string> GetAddress(List<long> IdLs);
        bool DeletePic(List<long> IdLs);

        string DeletePicEx(string Address);
    }

    /// <summary>
    /// 管理图片服务
    /// </summary>
    public class PicService : BaseService<Base_Picture, IPicRepository>, IPicService
    {
        /// <summary>
        /// 构造注入
        /// </summary>
        public PicService(IPicRepository repository) : base(repository)
        {

        }

        public List<PicDto> ShowPic(string PicGroup_No)
        {
            var data = this.repository_DB.GetList(a => a.PicGroupNo == PicGroup_No);
            return data.MapToList<Base_Picture, PicDto>();
        }

        public void SavePic(PicDto Entity)
        {
            if (Entity.Id < 1)
                this.Add(Entity);
            else
                this.Save(Entity);
        }

        public List<string> GetAddress(List<long> IdLs)
        {
            return this.repository_DB.GetPic_Address(IdLs);
        }

        public bool DeletePic(List<long> IdLs)
        {
            return this.repository_DB.DeletePic(IdLs);
        }

        public string DeletePicEx(string Address)
        {
            if (string.IsNullOrWhiteSpace(Address))
                return "";

            var data = this.repository_DB.GetEntity(a => a.PicUrl == Address, false);
            if (data == null)
                return "";

            if (this.repository_DB.Delete(data.Id))
                return data.PicAddress;
            else
                return "";
        }
    }
}
